from rest_framework import serializers

from uploads.models import Upload


class UploadSerializer(serializers.ModelSerializer):
    create_user_name = serializers.CharField(label='创建人用户名', help_text='创建人用户名', read_only=True,
                                             source='create_user.username')

    class Meta:
        model = Upload
        fields = ['id', 'file', 'file_name', 'file_size', 'file_type', 'create_time', 'create_user',
                  'create_user_name']
        extra_kwargs = {
            'file': {
                'write_only': True
            },
            'file_name': {
                'read_only': True
            },
            'file_size': {
                'read_only': True
            },
            'file_type': {
                'read_only': True
            },
            'create_time': {
                'read_only': True
            },
            'create_user': {
                'read_only': True
            },
        }

    def to_representation(self, instance):
        data = super().to_representation(instance)
        data['info'] = [data.get('file_size'), instance.file.url, data.get('file_type')]
        return data
